/*
 * @Author: maomao
 * @Date: 2021-03-26 10:55:00
 * @Description: 计算出生日期和年龄
 * @FilePath: \clientapp\src\utils\age_util.ts
 */
export class AgeUtil {
   /** 根据出生日期 算出几岁几月零几天 */
   static getAgeStr = (value) => {
      let d = new Date();
      let birthdays = new Date(value);
      let nowD = d.getFullYear() + "-" + (d.getMonth() + 1) + '-' + d.getDate();
      console.log('当前时间') + nowD;
      let nowE = birthdays.getFullYear() + '-' + (birthdays.getMonth() + 1) + '-' + birthdays.getDate();
      console.log('出生日期') + nowE;
      ///获取天数
      let day = d.getDate() - birthdays.getDate()
      console.log('相差天数' + day);
      ///获取月
      let month = d.getMonth() - birthdays.getMonth()
      console.log('相差月数' + month);
      ///获取年
      let year = d.getFullYear() - birthdays.getFullYear()
      console.log('相差月数' + year);
      ///根据出生日期的月份，算出下个月有几天
      console.log('-------------' + birthdays.getMonth());
      let nowDay = new Date(d.getFullYear(), birthdays.getMonth() + 2, 0).getDate();
      console.log('-------------天数' + nowDay);
      if (day < 0) {
         month -= 1;
         day = day + nowDay;
      }

      if (month < 0) {
         month = (month + 12) % 12;
         year--
      }
      let dateAge = year + '岁' + month + '月' + day + '天';
      return dateAge;

   }
   /** 根据出生日期 获取年龄 */
   static getAgeNumer = (value) => {
      let birthdays = new Date(value);
      let d = new Date();
      let age =
         d.getFullYear() -
         birthdays.getFullYear() -
         (d.getMonth() < birthdays.getMonth() ||
            (d.getMonth() == birthdays.getMonth() &&
               d.getDate() < birthdays.getDate())
            ? 1
            : 0);
      return age;
   }

   /** 根据身份证ID 获取 出生日期 */
   static getBirthDay = (idCard: string) => {
      let birthday = "";
      if (idCard != null && idCard != "") {
         if (idCard.length == 15) {
            birthday = "19" + idCard.substr(6, 6);
         } else if (idCard.length == 18) {
            birthday = idCard.substr(6, 8);
         }

         birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
      }
      return birthday;
   }

}

